#include "image.h"
#include "ui_image.h"
#include <QDebug>
#include <QLabel>

image::image(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::image)
{
    ui->setupUi(this);
}

image::~image()
{
    delete ui;
}

void image::imageLoad(const QString path)
{
    r_image.load(path);
}

void image::imageLoad(const QString path, int &_width, int &_height)
{
    r_image.load(path);
    _width = r_image.width();
    _height = r_image.height();
}

uchar *image::imageData()
{
    r_image_data = r_image.bits();
    return  r_image_data;
}

void image::iamgeItems(int x, int y, QList<QImage> &imageList)
{
    int r_width = r_image.width();
    int r_height =  r_image.height();
    int cellWidth = r_width / x;
    int cellHeight = r_height / y;
    qDebug() << r_width << " " <<  r_height << " " << cellWidth << " " << cellHeight;
    for(int i = 0 ;i < x; i++ )
    {
        for (int j = 0; j < y; j++) {
            QRect rect(i * cellWidth,j * cellHeight, cellWidth,cellHeight);
            qDebug() << rect.x() << " " <<  rect.y() << " " << rect.width() << " " << rect.height();
            _imageRects.push_back(rect);
            imageList.push_back(r_image.copy(rect));
        }
    }
    _imageList = imageList;
}

void image::iamgeItems(int x, int y, QList<uchar *> &list)
{
    int r_width = r_image.width();
    int r_height =  r_image.height();
    int cellWidth = r_width / x;
    int cellHeight = r_height / y;
    for(int i = 0 ;i < x; i++ )
    {
        for (int j = 0; j < y; j++) {
            QRect rect(i * cellWidth,j * cellHeight, cellWidth,cellHeight);
            qDebug() << rect.x() << " " <<  rect.y() << " " << rect.width() << " " << rect.height();
            _imageRects.push_back(rect);
            list.push_back(r_image.copy(rect).bits());
        }
    }
    _listItems = list;
}

void image::iamgeItems(int x, int y, QVector<uchar *> &vector)
{
    int r_width = r_image.width();
    int r_height =  r_image.height();
    int cellWidth = r_width / x;
    int cellHeight = r_height / y;
    for(int i = 0 ;i < x; i++ )
    {
        for (int j = 0; j < y; j++) {
            QRect rect(i * cellWidth,j * cellHeight, cellWidth,cellHeight);
            qDebug() << rect.x() << " " <<  rect.y() << " " << rect.width() << " " << rect.height();
            _imageRects.push_back(rect);
            vector.push_back(r_image.copy(rect).bits());
        }
    }
    _vectorItems = vector;
}

void image::imageSave()
{
    int i = 0;
    for(auto it : _imageList)
    {
        QString imageName = QString("image/%1.jpg").arg(i);
        it.save(imageName);
        i++;
    }
    qDebug() << "iamge save ok";
}

void image::imageSave(int index)
{
    _imageList.at(index).save(QString("image/%1.jpg").arg(index));
}

void image::imageSave(int start, int end)
{
    for(int i = start;i < end+1; i++)
    {
        QString imageName = QString("image/%1.jpg").arg(i);
        _imageList.at(i).save(imageName);
    }
    qDebug() << "iamge save ok";
}

void image::imageShow()
{
    for(int i = 0 ;i < _imageList.size() ; i++)
    {
        QLabel * label = new QLabel(this);
        label->setGeometry(_imageRects.at(i));
        QPixmap pixmap(QPixmap::fromImage(_imageList.at(i)));
        label->setPixmap(pixmap);
        label->show();
    }
}

void image::imageShow(QList<QImage> &listImage)
{

}

void image::imageShow(QList<uchar *> &listItems)
{

}

void image::imageShow(QVector<uchar *> &VectorItems)
{

}

image *image::instance()
{
    static image _image;
    return &_image;
}

